﻿using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Windows.Controls;
using System.Windows;

namespace PowersourceUIToolkit.Test
{
    /// <summary>
    /// Summary description for BootstrapperTest
    /// </summary>
    [TestClass]
    public class BootstrapperTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test Context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        private DummyPowersourceBootStrapper myBootstrapper;
        private TestViewModel myViewmodel;
        private IUIManager myManager;

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
         //Use TestInitialize to run code before running each test 
         [TestInitialize()]
         public void MyTestInitialize() 
         {
             myBootstrapper = new DummyPowersourceBootStrapper();
             myBootstrapper.TypesToLoad = new Type[] { this.GetType()};
             myBootstrapper.UIManager = new UIManager();
             myBootstrapper.Initialize();
             myViewmodel = ViewModelLocator.GetViewModel<TestViewModel>();
             myManager = (IUIManager)IoC.GetInstance(typeof(IUIManager), null);
         }
        //
        // Use TestCleanup to run code after each test has run
         [TestCleanup()]
         public void MyTestCleanup() 
         {
             myViewmodel = null;
             myManager = null;
             myBootstrapper.Dispose();
             myBootstrapper = null;
         }
        
        #endregion

        [TestMethod]
        public void TestViewLocation()
        {
            var view = myManager.GetOrCreateControl(myViewmodel);

            Assert.AreEqual(view.GetType(), typeof(TestView));
        }

        [TestMethod]
        public void TestTextBinding()
        {
            var view = myManager.GetOrCreateControl(myViewmodel);

            var textBlock = (TextBlock)view.FindName("Title");
            var textBlock2 = (TextBlock)view.FindName("tbTitle2");
            var textBox = (TextBox)view.FindName("tbTitle");
            //myViewmodel.Title = "TestTitle";
            //View.

            var bd = textBox.GetBindingExpression(TextBox.TextProperty);
            var pb = bd.ParentBinding;

            var TBlockbd = textBlock.GetBindingExpression(TextBlock.TextProperty);
            var TBlockpb = TBlockbd.ParentBinding;

            myViewmodel.Title = "TestTitle";
            //textBox.Text = "TestTitle";
            Window wnd = new Window();
            wnd.Content = view;
            bd.UpdateTarget();
            wnd.Show();
            wnd.Hide();
            Assert.AreEqual("TestTitle", myViewmodel.Title);
            Assert.AreEqual("TestTitle", textBlock.Text);
            Assert.AreEqual("TestTitle", textBox.Text);
            const string TestString2="Title changed through textbox";
            
            wnd.Show();
            textBox.Text = TestString2;
            bd.UpdateSource();
            
            wnd.Hide();
            Assert.AreEqual(TestString2, myViewmodel.Title);
            Assert.AreEqual(TestString2, textBlock.Text);
            wnd.Close();
            //Assert.AreEqual("TestTitle", textBox.Text);

        }
    }
}
